import tkinter as tk
from tkinter import filedialog, messagebox
from datetime import datetime
from PIL import Image, ImageTk
from tkcalendar import DateEntry
import json
import os
import re

def save_tasks(columns,frames,DATA_FILE):
    try:
        tasks = []
        for col in columns:
            for child in frames[col].winfo_children():
                if isinstance(child, tk.Frame):
                    task_data = {
                        'name': '',
                        'priority': '',
                        'due_date': '',
                        'image_path': '',
                    }
                    for widget in child.winfo_children():
                        if isinstance(widget, tk.Label):
                            text = widget.cget('text')
                            if text.startswith('Task:'):
                                task_data['name'] = text.split(': ')[1]
                            elif text.startswith('Priority:'):
                                task_data['priority'] = text.split(': ')[1]
                            elif text.startswith('Due Date:'):
                                task_data['due_date'] = text.split(': ')[1]
                        elif isinstance(widget, tk.PhotoImage):
                            # 假设你有一个方式获取图片路径
                            task_data['image_path'] = widget.file
                    tasks.append(task_data)
        save_data(tasks,DATA_FILE)
    except Exception as e:
        messagebox.showerror("Error saving tasks", str(e))

def save_data(tasks,DATA_FILE):
    try:
        with open(DATA_FILE, 'w') as file:
            json.dump(tasks, file)
    except Exception as e:
        messagebox.showerror("Error saving data", str(e))

def delete_card(card_frame):
    card_frame.destroy()
    save_tasks()  # 保存任务数据到JSON文件
